//
//  ClientModifyController.m
//  StarOrangeStaff
//
//  Created by lshenrong on 2020/6/23.
//  Copyright © 2020 lshenrong. All rights reserved.
//

#import "ClientModifyController.h"
#import "InputTempletTwelveTableViewCell.h"
#import "FSActionSheet.h"


@interface ClientModifyController ()<UITableViewDataSource,UITableViewDelegate,FSActionSheetDelegate,UITextFieldDelegate>

@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) CedarButton *comfirmBtn;
@property (nonatomic,strong) UILabel *titleLblFirst;
@property (nonatomic,strong) UILabel *titleLblSecond;
@property (nonatomic,strong) UILabel *deleteLbl;
@property (nonatomic,strong) NSString *identify;

@end

@implementation ClientModifyController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"客户详情";
    self.navigationItem.rightBarButtonItem = [UIConfigure barButtonItemWithTitle:@"编辑" target:self action:@selector(editAction)];
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.bottom.mas_equalTo(self.view);
    }];
    
    [self.tableView addSubview:self.comfirmBtn];
    [self.comfirmBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self.view.mas_bottom).mas_offset(-60);
        make.left.mas_equalTo(self.view).mas_offset(kMarginOffsetX);
        make.right.mas_equalTo(self.view).mas_offset(-kMarginOffsetX);
        make.height.mas_offset(40);
    }];
}

#pragma mark - Table view data source

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 2) {
        return 20.00f;
    }
    return 40.00f;

}

//section头部视图
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
   
    if (section == 0) {
        UIView *view = [[UIView alloc]init];
        view.backgroundColor = kAppBackgoundColor;
        [view addSubview:self.titleLblFirst];
        [self.titleLblFirst mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(view.mas_left).offset(15);
            make.centerY.equalTo(view.mas_centerY);
        }];
        return view;
    }else if (section == 1){
        UIView *view = [[UIView alloc]init];
        view.backgroundColor = kAppBackgoundColor;
        [view addSubview:self.titleLblSecond];
        [self.titleLblSecond mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(view.mas_left).offset(15);
            make.centerY.equalTo(view.mas_centerY);
        }];
        return view;
    }else {
        UIView *view = [[UIView alloc]init];
        view.backgroundColor = kAppBackgoundColor;
        return view;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    InputTempletTwelveTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"InputTempletTwelveTableViewCellIdentifier"];
    if (indexPath.section == 0) {
         if (indexPath.row == 0) {
               cell.placeholder = @" ";
               cell.cellTitle =  @"客户身份";
               cell.inputTextField.enabled =  NO;
             if (self.identify) {
                 cell.inputTextField.text = self.identify;
             }else {
                 cell.inputTextField.text = @"企业";
             }
               @weakify(self)
               cell.textChangeBlock = ^(NSInteger tag, id body) {
                   @strongify(self)
                   
               };
           }else if (indexPath.row == 1) {
               cell.placeholder = @"请输入完整的公司名称";
               cell.cellTitle =  @"单位名称";
               cell.inputTextField.keyboardType =  UIKeyboardTypeDefault;
               @weakify(self)
               cell.textChangeBlock = ^(NSInteger tag, id body) {
                   @strongify(self)
                   
               };
           }else if (indexPath.row == 2) {
               cell.placeholder = @"请填写部门名称（可不填）";
               cell.cellTitle =  @"所属部门";
               cell.inputTextField.keyboardType =  UIKeyboardTypeDefault;
               @weakify(self)
               cell.textChangeBlock = ^(NSInteger tag, id body) {
                   @strongify(self)
                   
               };
           }else if (indexPath.row == 3) {
               cell.placeholder = @"请输入姓名";
               cell.cellTitle =  @"联系人";
               cell.inputTextField.keyboardType =  UIKeyboardTypeDefault;
               @weakify(self)
               cell.textChangeBlock = ^(NSInteger tag, id body) {
                   @strongify(self)
                   
               };
           }else if (indexPath.row == 4) {
               cell.placeholder = @"请输入联系人号码";
               cell.cellTitle =  @"联系方式";
               cell.inputTextField.keyboardType =  UIKeyboardTypeDefault;
               @weakify(self)
               cell.textChangeBlock = ^(NSInteger tag, id body) {
                   @strongify(self)
                   
               };
           }else if (indexPath.row == 5) {
               cell.placeholder = @"请输入公司地址";
               cell.cellTitle =  @"公司地址";
               cell.inputTextField.delegate = self;
               cell.inputTextField.keyboardType =  UIKeyboardTypeDefault;
               @weakify(self)
               cell.textChangeBlock = ^(NSInteger tag, id body) {
                   @strongify(self)
                   
               };
           }
        
        return cell;
    }else if (indexPath.section == 1){
        if (indexPath.row == 0) {
                     cell.placeholder = @"请输入发票抬头";
                     cell.cellTitle =  @"发票抬头";
                     cell.inputTextField.keyboardType =  UIKeyboardTypeDefault;
                     @weakify(self)
                     cell.textChangeBlock = ^(NSInteger tag, id body) {
                         @strongify(self)
                         
                     };
                 }else if (indexPath.row == 1) {
                     cell.placeholder = @"请输入纳税人识别码";
                     cell.cellTitle =  @"纳税人识别码";
                     cell.inputTextField.keyboardType =  UIKeyboardTypeDefault;
                     cell.titleLenth = 120;
                     @weakify(self)
                     cell.textChangeBlock = ^(NSInteger tag, id body) {
                         @strongify(self)
                         
                     };

                 }
        return cell;


    }else {
        if (indexPath.row == 0) {
             UITableViewCell *lastCell = [tableView dequeueReusableCellWithIdentifier:@"lastCellIdentifier"];
                   [lastCell.contentView addSubview:self.deleteLbl];
                   [self.deleteLbl mas_updateConstraints:^(MASConstraintMaker *make) {
                       make.left.mas_equalTo(lastCell.contentView).mas_offset(kMarginOffsetX);
                       make.centerY.mas_equalTo(lastCell.contentView.mas_centerY);
                   }];
            return lastCell;
        }
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            [self selectcClientIdentify:indexPath];
        }
    }
}


 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
     return  [InputTempletTwelveTableViewCell heightForCell];
     
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return   3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 6;
    }else if (section == 1) {
        return 2;
    }else {
        return 1;
    }
    return 0;
}

- (void)selectcClientIdentify:(NSIndexPath *)indexPath {
    
    NSMutableArray *actionSheetItems = [@[FSActionSheetTitleItemMake(FSActionSheetTypeNormal, @"企业"),
                                          FSActionSheetTitleItemMake(FSActionSheetTypeNormal, @"政府单位"),
                                          FSActionSheetTitleItemMake(FSActionSheetTypeNormal, @"旅行社"),FSActionSheetTitleItemMake(FSActionSheetTypeNormal, @"个人"),]
                                        mutableCopy];
    FSActionSheet *actionSheet = [[FSActionSheet alloc] initWithTitle:nil cancelTitle:nil items:actionSheetItems];
    // 展示并绑定选择回调
    [actionSheet showWithSelectedCompletion:^(NSInteger selectedIndex) {
        FSActionSheetItem *item = actionSheetItems[selectedIndex];
        self.identify = item.title;
        [self.tableView reloadData];
    }];
    
}

- (void)FSActionSheet:(FSActionSheet *)actionSheet selectedIndex:(NSInteger)selectedIndex {
    
    
}

- (void)textFieldDidChange:(UITextField *)textField {
   CGFloat cellHeight = [self heightForCellWithText:textField.text];

}

- (CGFloat)heightForCellWithText:(NSString *)text{
    if (IsEmptyStr(text)) {
        return 0.0;
    }
    CGFloat maxSize = SCREEN_WIDTH -  2*kMarginOffsetX ;
    UIFont *font = PingFangZhongChangGuiForFont(14);
    CGSize size = [PlatformTools sizeWithString:text andFount:font andMaxSize:CGSizeMake(maxSize, MAXFLOAT)];
    return size.height + 53;
}

 

- (CedarButton *)comfirmBtn{
    if (!_comfirmBtn){
        _comfirmBtn =  [CedarButton buttonWithTextForGradientForCommomn:@"邀请客户" radius:YES];
        @weakify(self)
        _comfirmBtn.clickEvent = ^{
            @strongify(self)
        };
    }
    return _comfirmBtn;
}

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [UIConfigure tableViewFromFactory];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.rowHeight = 250;
        _tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 0)];
        [_tableView registerClass:[InputTempletTwelveTableViewCell class] forCellReuseIdentifier:@"InputTempletTwelveTableViewCellIdentifier"];
        [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"lastCellIdentifier"];

    }
    return _tableView;
}

MACRO_UILabelWithPFCGFont(deleteLbl,@"删除此用户",kRedColor,18)
MACRO_UILabelWithPFCGFont(titleLblFirst,@"基本信息",kGrayColor,15)
MACRO_UILabelWithPFCGFont(titleLblSecond,@"发票信息",kGrayColor,15)


@end

